/*
 * All rights Reserved, Designed By www.goldbeacon.cn
 * @Title:  demo
 * @Package cn.goldbeacon.demo.domain
 * @author: zoujie
 * @date:   2021/8/1119:36
 * @version V1.0
 * Copyright © 2021 www.goldbeacon.cn. All Rights Reserved.
 * 注意：本内容仅限于湖南金烽信息科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package com.chenzhengrong.demospringboot.domain;


import com.chenzhengrong.demospringboot.enums.HttpStatusEnum;
import com.chenzhengrong.demospringboot.util.I18nUtils;
import lombok.Data;

import java.io.Serializable;

/**
 * @author zoujie
 * @title: R
 * @description: 响应信息主体
 * @date 2021/8/11 19:36
 */
@Data
public class R<T> implements Serializable {

    /** 序列化版本号 */
    private static final long serialVersionUID = -2894842850201749361L;

    private int code;

    private String msg;

    private T data;

    /**
    * @description: 返回成功消息
    * @return cn.goldbeacon.demo.domain.R<T>
    * @author zoujie
    * @date 2021/8/11 19:39
    */
    public static <T> R<T> ok() {
        return restResult(HttpStatusEnum.SUCCESS, null);
    }

    /**
    * @description: 返回成功消息，带数据
    * @param data 数据
    * @return cn.goldbeacon.demo.domain.R<T>
    * @author zoujie
    * @date 2021/8/11 19:39
    */
    public static <T> R<T> ok(T data) {
        return restResult(HttpStatusEnum.SUCCESS, data);
    }

    /**
    * @description: 返回成功消息，带数据和自定义消息
    * @param data 数据
    * @param msg 消息
    * @return cn.goldbeacon.demo.domain.R<T>
    * @author zoujie
    * @date 2021/8/11 19:40
    */
    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, HttpStatusEnum.SUCCESS.getCode(), msg);
    }

    /**
    * @description: 返回失败消息
    * @return cn.goldbeacon.demo.domain.R<T>
    * @author zoujie
    * @date 2021/8/11 19:40
    */
    public static <T> R<T> fail() {
        return restResult(HttpStatusEnum.ERROR, null);
    }

    /**
    * @description: 返回失败消息，自定义消息
    * @param msg 消息
    * @return cn.goldbeacon.demo.domain.R<T>
    * @author zoujie
    * @date 2021/8/11 19:41
    */
    public static <T> R<T> fail(String msg) {
        return restResult(null, HttpStatusEnum.ERROR.getCode(), msg);
    }

    /**
    * @description: 返回失败消息
    * @param httpStatusEnum
    * @return cn.goldbeacon.demo.domain.R<T>
    * @author zoujie
    * @date 2021/8/11 20:30
    */
    public static <T> R<T> fail(HttpStatusEnum httpStatusEnum) {
        return restResult(null, httpStatusEnum.getCode(), httpStatusEnum.getMsg());
    }

    /**
    * @description: 返回失败消息，带数据
    * @param data 数据
    * @return cn.goldbeacon.demo.domain.R<T>
    * @author zoujie
    * @date 2021/8/11 19:41
    */
    public static <T> R<T> fail(T data) {
        return restResult(HttpStatusEnum.ERROR, data);
    }

    /**
    * @description: 返回失败消息，带数据自定义消息
    * @param data 数据
    * @param msg 消息
    * @return cn.goldbeacon.demo.domain.R<T>
    * @author zoujie
    * @date 2021/8/11 19:42
    */
    public static <T> R<T> fail(T data, String msg) {
        return restResult(data, HttpStatusEnum.ERROR.getCode(), msg);
    }

    /**
    * @description: 返回失败消息，自定义代码和消息
    * @param code 代码
    * @param msg 消息
    * @return cn.goldbeacon.demo.domain.R<T>
    * @author zoujie
    * @date 2021/8/11 19:42
    */
    public static <T> R<T> fail(int code, String msg) {
        return restResult(null, code, msg);
    }

    /**
    * @description: 重置消息报文
    * @param data 数据
    * @param code 代码
    * @param msg 消息
    * @return cn.goldbeacon.demo.domain.R<T>
    * @author zoujie
    * @date 2021/8/11 19:43
    */
    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        //apiResult.setMsg(I18nUtils.getMessage(msg));
        apiResult.setMsg(msg);
        return apiResult;
    }

    /**
    * @description: 重置消息报文
    * @param httpStatusEnum 枚举类
    * @param data 数据
    * @return cn.goldbeacon.demo.domain.R<T>
    * @author zoujie
    * @date 2021/8/12 15:25
    */
    private static <T> R<T> restResult(HttpStatusEnum httpStatusEnum, T data) {
        R<T> apiResult = new R<>();
        apiResult.setCode(httpStatusEnum.getCode());
        //apiResult.setMsg(I18nUtils.getMessage(httpStatusEnum.getMsg()));
        apiResult.setData(data);
        return apiResult;
    }
}
